package com.kgc.controller.admin;

import com.kgc.common.Result;
import com.kgc.entity.Role;
import com.kgc.entity.User;
import com.kgc.service.interf.admin.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hyj
 * @time 2024/4/6 14:23
 * @describe
 */
@RestController
@RequestMapping("/apis/admin/role")
public class AdminRoleController {
    @Autowired
    private AdminRoleService adminRoleService;

    @RequestMapping("/roles")
    public String roles() {
        List<Role> roles = adminRoleService.roles();
        return Result.success("0", "查询成功", roles);
    }

    @RequestMapping("/getAllUserRoles")
    public String getAllUserRoles() {
        List<User> all = adminRoleService.getAll();
        return Result.success("0", "查询成功", all);
    }

    @RequestMapping("/changeRole")
    public String changeRole(Integer userId, Integer[] roleIds) {
        String string = adminRoleService.updateRole(userId, roleIds);
        if (string.equals("更新成功")) {
            return Result.success("0", "更新成功");
        }
        return Result.success("1", "更新失败");
    }

    @RequestMapping("/addRole")
    public String addRole(String roleName) {
        Integer i = adminRoleService.insertRole(roleName);
        if (i == 1) {
            return Result.success("0", "添加成功");
        }
        return Result.success("1", "添加失败");
    }

    @RequestMapping("/updateUserAndRole")
    public String updateUserAndRole(Integer userId, Integer roleId) {
        Integer i = adminRoleService.updateUserAndRole(userId, roleId);
        if (i == 1) {
            return Result.success("0", "更新成功");
        }
        return Result.success("1", "更新失败");
    }

    @RequestMapping("/insertUserAndRole")
    public String insertUserAndRole(Integer userId, Integer roleId) {
        Integer i = adminRoleService.insertUserAndRole(userId, roleId);
        if (i == 1) {
            return Result.success("0", "添加成功");
        }
        return Result.success("1", "添加失败");
    }

    @RequestMapping("/deleteUserAndRole")
    public String deleteUserAndRole(Integer userId, Integer roleId) {
        Integer i = adminRoleService.deleteUserAndRole(userId, roleId);
        if (i == 1) {
            return Result.success("0", "删除成功");
        }
        return Result.success("1", "删除失败");
    }
}
